using System;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using ctsu.BaseForms;
using ctsu.Admin.Forms;

namespace ctsu.Admin.BusLogic
{
	public class BLFormReportTimePeriodBaseImpl : IBLFormReportTimePeriodBase
	{
		private FormReportTimePeriodBase _formReport;
		private CriteriaInfo _criteria;
		private TitleInfo _title;
		private FormReportInfo _reportMeta;
		private bool _isGridInitialized;
		private DataView _reportView;
		private DataTable _reportData;

		public BLFormReportTimePeriodBaseImpl()
		{
		}

		protected virtual bool VerifyDates(out string message)
		{

			message = "";
			if (_formReport.BeginPeriodDate == CremaConstants.NullDate ||
				_formReport.EndPeriodDate == CremaConstants.NullDate)
			{
				message = "Enter both a Begin Date and an End Date";
				return false;
			}

			if (_formReport.BeginPeriodDate.CompareTo(_formReport.EndPeriodDate) > -1)
			{
				message = "End Date must be equal to or greater than Begin Date";
				return false;
			}
			return true;
		}
		#region IBLFormReportBase Members
		public FormReportTimePeriodBase FormReport
		{
			get { return _formReport; }
			set { _formReport = value; }
		}
		public CriteriaInfo Criteria
		{
			get { return _criteria; }
			set { _criteria = value; }
		}

		public TitleInfo Title
		{
			get { return _title; }
			set { _title = value; }
		}

		public bool IsGridInitialized
		{
			get { return _isGridInitialized; }
			set { _isGridInitialized = value; }
		}

		public FormReportInfo ReportMeta
		{
			get { return _reportMeta; }
			set { _reportMeta = value; }
		}

		public DataView ReportView
		{
			get { return _reportView; }
			set { _reportView = value; }
		}

		public DataTable ReportData
		{
			get { return _reportData; }
			set { _reportData = value; }
		}

		public virtual bool Print()
		{
			StringBuilder sbCrit = new StringBuilder();

			sbCrit.Append("Criteria: ");
			string fromDate = "";
			fromDate = _formReport.BeginPeriodDate.ToShortDateString();
			string toDate = "";
			toDate = _formReport.EndPeriodDate.ToShortDateString();

			sbCrit.Append("  From Date: " + fromDate);
			sbCrit.Append("  To Date: " + toDate);
			sbCrit.Append("  Count: " + _formReport.Count.Text);
			string[] title = new string[2];
			title[0] = this._reportMeta.Title;
			title[1] = sbCrit.ToString();
			DataView dv = (DataView)this._formReport.Grid.DataSource;
			PrintingUtils pu = new PrintingUtils(dv, title, this._formReport, this._reportMeta.Landscape);
			pu.Initialize();

			return true;
		}

		public bool Initialize()
		{
			//size grid columns			
			this._formReport.Grid.DataSource = this._reportData;
			DataGridTableStyle dgts = new DataGridTableStyle();
			dgts.MappingName = "Report";
			this._formReport.Grid.TableStyles.Add(dgts);

			ReportColumnInfo rci;
			for (int i = 0; i < this._reportData.Columns.Count; i++)
			{

				//this is for the grid
				rci = (ReportColumnInfo)this._reportMeta.ReportColumns[i];
				dgts.GridColumnStyles[i].Width = rci.ColumnWidth;
				dgts.GridColumnStyles[i].HeaderText = rci.DisplayName;
				//this is for printing
				//print utils only has the data table for meta data
				this._reportData.Columns[i].ExtendedProperties.Add("Width", rci.ColumnWidth);
				this._reportData.Columns[i].Caption = rci.DisplayName;
			}
			_isGridInitialized = true;
			return true;
		}

		public void GenericAction(Control sender, EventArgs e)
		{
		}

		public virtual bool Run()
		{
			Console.WriteLine("BL RUN!");
			string message = "";
			if (!VerifyDates(out message))
			{
				MessageBox.Show(this._formReport, message, "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return true; ;
			}

			this._formReport.Grid.DataSource = null;
			this._formReport.btnPrint.Enabled = false;
			//use reflection to call the method
			int pos = this.ReportMeta.MethodName.LastIndexOf(".");
			string methodClass = this.ReportMeta.MethodName.Substring(0, pos);
			string method = this.ReportMeta.MethodName.Substring(pos + 1);
			Type type;
			MethodInfo mi;

			//we alread know the param type so we don't need this
			//if we decide to go with dynamic parameters then we will use this
			//			Type[] paramTypes = new Type[2];
			//			paramTypes[0] = typeof(DateTime);
			//			paramTypes[1] = typeof(DateTime);						
			type = Type.GetType(methodClass);
			mi = type.GetMethod(method);

			object[] paramObj = new object[1];
			FormReportParamInfo reportParamInfo = new FormReportParamInfo();
			reportParamInfo.BeginDate = this._formReport.BeginPeriodDate;
			reportParamInfo.EndDate = this._formReport.EndPeriodDate;
			reportParamInfo.SPName = this._reportMeta.SPName;
			paramObj[0] = reportParamInfo;
			this._reportData = (DataTable)mi.Invoke(type, paramObj);
			//this._reportData = ArpUtils.GetActiveProtocolsForDates(this._formReport.BeginPeriodDate,this._formReport.EndPeriodDate);
			if (_reportData == null)
				MessageBox.Show(this._formReport, "No records found for this criteria.", "No Records Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

			if (!_isGridInitialized)
				Initialize();
			this._reportView = new DataView(this._reportData);
			this._formReport.Grid.DataSource = this._reportView;
			this._formReport.Count.Text = this._reportData.Rows.Count.ToString();
			if (this._reportData.Rows.Count > 0)
				_formReport.btnPrint.Enabled = true;

			return false;
		}

		public bool Exit()
		{
			this._formReport.Close();
			return true;
		}
		#endregion
	}
}
